﻿using System;
using System.Collections.Generic;
using System.Linq;
using AMM.CustomClassLibrary;
using AMM.DataAccess.Store;
using AMM.Model;
using AMM.Model.Store;

namespace AMM.Business.Store {
    public class ProductInventoryProcessor : BaseProcessor {

        private int _pageSize;
        private ProductInventoryRepository _productInventoryRepository;

        public ProductInventoryProcessor(int pageSize = int.MaxValue) {
            this._pageSize = pageSize;
            this._productInventoryRepository = new ProductInventoryRepository(pageSize);
        }

        public ProductInventory FetchProductInventoryById(int productInventoryId) {
            try {
                return this._productInventoryRepository.FetchProductInventoryById(productInventoryId);
            }
            catch (Exception ex) {
                Logger.Error(ex.Message, ex);
                throw this.BuildException("获取指定成品盘点单失败！");
            }
        }

        public List<ProductInventoryViewModel> FetchProductInventoryViewModelList(ProductInventorySearchModel searchModel, int pageIndex = 1) {
            try {
                List<ProductInventory> resultList = this._productInventoryRepository.FetchProductInventoryList(searchModel, pageIndex);
                return resultList.Select(x => {
                    return new ProductInventoryViewModel() {
                        Key = x.ProductInventoryId,
                        ProductCode = x.Product.Code,
                        ProductName = x.Product.Name,
                        StoreBasisCode = x.StoreBasi.Code,
                        StoreBasisName = x.StoreBasi.Name,
                        PreQuantity = x.PreQuantity,
                        Quantity = x.Quantity,
                        SpanQuantity = x.SpanQuantity.Value,
                        Remarks = x.Remarks,
                        CreatedBy = x.CreatedBy,
                        CreatedDatetime = x.CreatedDatetime
                        //,ModifiedBy = x.ModifiedBy,
                        //ModifiedDatetime = x.ModifiedDatetime
                    };
                }).ToList();
            }
            catch (Exception ex) {
                Logger.Error(ex.Message, ex);
                throw this.BuildException("获取成品盘点单列表失败！");
            }
        }

        public void Insert(ProductInventory productInventory) {
            try {
                productInventory.ProductInventoryId = Guid.NewGuid().GetHashCode();
                productInventory.CreatedDatetime = DateTime.Now;
                productInventory.SpanQuantity = productInventory.Quantity - productInventory.PreQuantity;
                this._productInventoryRepository.InsertProductInventory(productInventory);
            }
            catch (Exception ex) {
                Logger.Error(ex.Message, ex);
                throw this.BuildException("插入新成品盘点单失败！");
            }
        }

        public void Update(ProductInventory productInventory) {
            try {
                ProductInventory oldProductInventory = this.FetchProductInventoryById(productInventory.ProductInventoryId);
                if (!oldProductInventory.RowVersion.SequenceEqual(productInventory.RowVersion)) {
                    throw this.BuildException("更新成品盘点单失败，数据不一致性错误！");
                }
                productInventory.ModifiedDatetime = DateTime.Now;
                this._productInventoryRepository.Update(productInventory);
            }
            catch (Exception ex) {
                Logger.Error(ex.Message, ex);
                throw this.BuildException("更新成品盘点单失败！");
            }
        }


        public void Delete(ProductInventory productInventory) {
            try {
                this._productInventoryRepository.Delete(productInventory);
            }
            catch (Exception ex) {
                Logger.Error(ex.Message, ex);
                throw this.BuildException("删除成品盘点单失败！");
            }
        }

        public void Delete(params int[] productInventoryIds) {
            try {
                this._productInventoryRepository.DeleteMultipleProductInventory(productInventoryIds);
            }
            catch (Exception ex) {
                Logger.Error(ex.Message, ex);
                throw this.BuildException("删除成品盘点单失败！");
            }
        }
    }
}
